package pkg16b_eca.SQL.Tables;

import java.util.ArrayList;
import java.util.HashMap;
import pkg16b_eca.SQL.Query;
import pkg16b_eca.data.validate.Text;
import se.oru.informatik.InformatikException;

public class SQLFormaga extends Query {
    public SQLFormaga() throws InformatikException {}
    
    /** Inserting values into database. */
    public void insert(final String strNamn, final String strBeskrivning) {
        String query = "insert into formaga (FID, namn, beskrivning) values (" + 
                SQLGetAutoIncrement("formaga", "FID") + ", '" + strNamn + "', '" + strBeskrivning + "')";
        SQLInsert(query);
    }
    
    /** Inserting values into database. */
    public void update(final int intFID, final String strNamn, final String strBeskrivning) {
        String query = "update formaga set namn='" + strNamn + "', beskrivning='" + strBeskrivning + "' where FID=" + intFID;
        SQLUpdate(query);
    }
    
    /** Deleting a value from the database. */
    public void delete(final int intFID) {
        String query = "delete from formaga where FID=" + intFID;
        SQLDelete(query);
    }
    
    /** Check if a value exists. */
    public boolean exists(final String FName) {
        String query = SQLSelect("select namn from formaga where namn = '" + FName + "'");
        return !query.isEmpty();
    }
    
    /** Check if a value exists. */
    public boolean exists(final int FID) {
        String query = SQLSelect("select FID from formaga where FID = '" + FID + "'");
        return !query.isEmpty();
    }
    
    /**Usage Example : Se vilka förmågor som finns i systemet
     *Variables used: alhm = ArrayListHashMap, hm = HashMap
        ArrayList<HashMap<String, String>> alhm = SQL.getFormaga().getFormagor();
        for (HashMap<String, String> hm : alhm) {
            System.out.println(hm.values());
        }
     */
    public ArrayList<String> getFormagor() {
        return SQLSelectList("select formaga.namn from formaga");
    }
    
    /** Select all förmågor. */
    public ArrayList<HashMap<String, String>> getAllFormagor() {
        return SQLSelectRows("select * from formaga");
    }
    
    //Returnera antal förmågor som existerar.
    public int getFormagorCount() {
        return Text.parseNumber(SQLSelect("select count(fid) from formaga"));
    }
    
    /** Select a förmågas name from an ID. */
    public String getFormagaName(int FID) {
        return SQLSelect("select formaga.namn from formaga where formaga.fid = " + FID);
    }
    
    /** Select a förmågas ID from a name. */
    public int getFormagaFID(String name) {
        return Text.parseNumber(SQLSelect("select formaga.fid from formaga where formaga.namn = '" + name + "'"));
    }
}
